package com.cleaningcloud.erp.controller;

import com.cleaningcloud.common.core.utils.poi.ExcelUtil;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.core.web.page.TableDataInfo;
import com.cleaningcloud.common.log.annotation.Log;
import com.cleaningcloud.common.log.enums.BusinessType;
import com.cleaningcloud.common.security.annotation.RequiresPermissions;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.api.domain.ErpCommodity;
import com.cleaningcloud.erp.domain.dto.ErpCommodityDto;
import com.cleaningcloud.erp.domain.vo.ErpCommodityVo;
import com.cleaningcloud.erp.service.IErpCommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 商品管理Controller
 *
 * @author cleaningcloud
 * @date 2024-09-27
 */
@RestController
@RequestMapping("/ErpCommodity")
public class ErpCommodityController extends BaseController {
    @Autowired
    private IErpCommodityService erpCommodityService;

    /**
     * 查询商品管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ErpCommodityDto erpCommodity) {
        startPage();
        List<ErpCommodityVo> list = erpCommodityService.selectErpCommodityList(erpCommodity);
        return getDataTable(list);
    }

    /**
     * 导出商品管理列表
     */
    @RequiresPermissions("erp:ErpCommodity:export")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpCommodityDto erpCommodity) {
        List<ErpCommodityVo> list = erpCommodityService.selectErpCommodityList(erpCommodity);
        ExcelUtil<ErpCommodityVo> util = new ExcelUtil<ErpCommodityVo>(ErpCommodityVo.class);
        util.exportExcel(response, list, "商品数据");
    }

    @Log(title = "商品管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("erp:ErpCommodity:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ErpCommodityVo> util = new ExcelUtil<ErpCommodityVo>(ErpCommodityVo.class);
        List<ErpCommodityVo> commodityList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = erpCommodityService.importErpCommodity(commodityList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<ErpCommodityVo> util = new ExcelUtil<ErpCommodityVo>(ErpCommodityVo.class);
        util.importTemplateExcel(response, "商品数据");
    }

    /**
     * 获取商品管理详细信息
     */
    @RequiresPermissions("erp:ErpCommodity:query")
    @GetMapping(value = "/{commodityId}")
    public AjaxResult getInfo(@PathVariable("commodityId") Long commodityId) {
        return success(erpCommodityService.selectErpCommodityByCommodityId(commodityId));
    }

    /**
     * 新增商品管理
     */
    @RequiresPermissions("erp:ErpCommodity:add")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpCommodityDto erpCommodity) {
        return toAjax(erpCommodityService.insertErpCommodity(erpCommodity));
    }

    /**
     * 修改商品管理
     */
    @RequiresPermissions("erp:ErpCommodity:edit")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpCommodityDto erpCommodity) {
        return toAjax(erpCommodityService.updateErpCommodity(erpCommodity));
    }

    /**
     * 删除商品管理
     */
    @RequiresPermissions("erp:ErpCommodity:remove")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{commodityIds}")
    public AjaxResult remove(@PathVariable Long[] commodityIds) {
        return toAjax(erpCommodityService.deleteErpCommodityByCommodityIds(commodityIds));
    }

    /**
     * 查询商品管理列表
     */
    @GetMapping("/getList")
    public AjaxResult getList(ErpCommodityDto erpCommodity) {
        List<ErpCommodityVo> list = erpCommodityService.selectErpCommodityList(erpCommodity);
        return success(list);
    }

}
